package flyrestaurant.jcy.ui;

import java.io.IOException;
import java.util.Properties;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.jcy.food.R;

import flyrestaurant.jcy.App;
import flyrestaurant.jcy.entity.User;
import flyrestaurant.jcy.service.ConfigService;
import flyrestaurant.jcy.service.UserService;

public class LoginActivity extends BasicActivity {

	EditText serverUrlEdt;
	EditText codeEdt;
	EditText passwordEdt;
	Button loginBtn;
	Button cancelBtn;
	
	UserService userService;
	ConfigService configService;
	Properties config;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		String title = getString(R.string.app_name) + " " + getName();
		setTitle(title);
		
		userService = new UserService(this);
		configService = new ConfigService(this);
		
		serverUrlEdt = (EditText)findViewById(R.id.serverUrlEdt);
		codeEdt = (EditText)findViewById(R.id.codeEdt);
		passwordEdt = (EditText)findViewById(R.id.passwordEdt);
		loginBtn = (Button)findViewById(R.id.loginBtn);
		cancelBtn = (Button)findViewById(R.id.cancelBtn);
		
//		LoginButtonListener loginButtonListener = new LoginButtonListener();
//		loginBtn.setOnClickListener(loginButtonListener);
		
		try {
			config = configService.read();
			serverUrlEdt.setText(config.getProperty("serverUrl"));
		} catch (IOException e) {
			e.printStackTrace();
			showMessageDialog("配置文件读取失败"+e.getMessage(), R.drawable.not, null);
			config = new Properties();
		}
		
		loginBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String serverUrl = serverUrlEdt.getText().toString();
				String code = codeEdt.getText().toString();
				String password = passwordEdt.getText().toString();
				
				if (serverUrl.length() == 0) {
					showMessageDialog("请输入服务器地址", R.drawable.warning, null);
					return;
				}
				
				if(code.length() == 0 || password.length() == 0 ) {
					showMessageDialog("请输入登录名和密码", R.drawable.warning, null);
					return;
				}
				config.put("serverUrl", serverUrl);
				App app = (App) getApplicationContext();
				app.serverUrl = serverUrl;
			
				try {
					User user = userService.login(code, password);
					if (user == null) {
						showMessageDialog("登陆名或密码错误", R.drawable.warning, null);
						return;
					}
					try {
						configService.write(config);
					} catch (IOException e) {
						e.printStackTrace();
						showMessageDialog("配置文件保存失败"+e.getMessage(),R.drawable.not,null);
					}
					app.user = user;
					showActivity(LoginActivity.this, MainActivity.class);
				} catch (Exception e) {
					e.printStackTrace();
					showMessageDialog("登陆失败"+e.getMessage(), R.drawable.not, null);
				}
			}
		});
		
		cancelBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				LoginActivity.this.finish();
			}
		});
	}
	

	@Override
	protected int getLayoutId() {
		return R.layout.login;
	}

	@Override
	protected String getName() {
		return "登陆";
	}
	
}
